package rulesControl;

import gameBasicElements.*;
import gameEngine.GameHistory;

/**
 * @author Tomasz Wiatrowski
 *
 */
public enum MovementApprovers
{
	ValidSquare
	{

		@Override
		public boolean approve(GameHistory history, Move move) {
			if(move.passed())
				return true;
			Board curBoard = history.getBoardAtTurn(history.getCurrentTurn());
			int size = curBoard.getSize();
			Position movePosition = move.getPosition();
			return (curBoard.getPlayerColor(movePosition)==null&&movePosition.getX()>=0&&movePosition.getX()<size&&movePosition.getY()>=0&&movePosition.getY()<size);
		}

		@Override
		public boolean optional() {
			return false;
		}
		
		@Override
		public String toString()
		{
			return "Zajęte pole";
		}
	},
	KoRule
	{

		@Override
		public boolean approve(GameHistory history, Move move) {
			if(move.passed())
				return true;
			int curTurn = history.getCurrentTurn();
			if(curTurn<2)
				return true;
			Board curBoard = history.getBoardAtTurn(curTurn);
			curBoard = curBoard.update(move);
			Board prevBoard = history.getBoardAtTurn(curTurn-1);
			return !curBoard.equals(prevBoard);
		}

		@Override
		public boolean optional() {
			return false;
		}
		@Override
		public String toString()
		{
			return "Zasada Ko";
		}
	},
	SuicideProtector
	{

		@Override
		public boolean approve(GameHistory history, Move move) {
			if(move.passed())
				return true;
			Board prevBoard = history.getBoardAtTurn(history.getCurrentTurn());
			Board curBoard = prevBoard.update(move);
			for(int i=0; i<curBoard.getSize(); ++i)
				for(int j=0; j<curBoard.getSize(); ++j)
					curBoard.setPlayerColor(new Position(i, j), prevBoard.getPlayerColor(new Position(i, j)));
			Position movePosition = move.getPosition();
			curBoard.setPlayerColor(movePosition, move.getColor());
			for(Position pos : curBoard.getNeighbours(movePosition)){
				if(curBoard.getPlayerColor(pos)!=move.getColor()&&curBoard.getPlayerColor(pos)!=null)
					curBoard.takeCaptives(curBoard.getCaptives(pos));
			} 
			for(Position pos : curBoard.getNeighbours(movePosition)){
				if((curBoard.getPlayerColor(pos) == curBoard.getPlayerColor(movePosition)) || (curBoard.getPlayerColor(pos) == null))
					return true;
			}
			return false;
		}

		@Override
		public boolean optional() {
			return false;
		}
		@Override
		public String toString()
		{
			return "Brak samobójstw";
		}
	},
	MassSuicideProtector
	{

		@Override
		public boolean approve(GameHistory history, Move move) {
			if(move.passed())
				return true;
			Board curBoard = history.getBoardAtTurn(history.getCurrentTurn());
			curBoard = curBoard.update(move);
			if(curBoard.getPlayerColor(move.getPosition()) == null)
				return false;
			return true;
		}

		@Override
		public boolean optional() {
			return true;
		}
		
		@Override
		public String toString()
		{
			return "Masowe samobójstwo";
		}
	},
	SuperKoRule
	{

		@Override
		public boolean approve(GameHistory history, Move move) {
			if(move.passed())
				return true;
			int curTurn = history.getCurrentTurn();
			Board curBoard = history.getBoardAtTurn(curTurn);
			curBoard = curBoard.update(move);
			for(int i=0; i<curTurn; i++){
				Board prevBoard = history.getBoardAtTurn(curTurn);
				if(curBoard.equals(prevBoard))
					return false;
			}
			return true;
		}

		@Override
		public boolean optional() {
			return true;
		}
		
		@Override
		public String toString()
		{
			return "SuperKo";
		}
	};
	public abstract boolean approve(GameHistory history, Move move);
	public abstract boolean optional();
}
